//
//  HomeView.m
//  0.3camera
//
//  Created by 茶 on 2023/11/23.
//

#import "HomeView.h"

@interface HomeView () <STDSRenderViewDelegate>

@property (nonatomic, strong, readwrite) STDSRenderView *displayView;
@property (nonatomic, strong, readwrite) STDSGLCompseParam *compose;


@property (nonatomic, strong) UILabel *numLbl;

@end

@implementation HomeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initInterface];
    }
    return self;
}

- (void)initInterface {
    CGFloat height = self.width * (640.0 / 480.0);
    _displayView = [[STDSRenderView alloc] initWithFrame:CGRectMake(0, 0, self.width, height) composeSize:CGSizeMake(self.width, height)];
    _displayView.delegate = self;
    [self addSubview:self.displayView];
    [_displayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(height);
        make.centerY.mas_equalTo(0);
    }];
    
    UIView *contentView = [UIView new];
    contentView.backgroundColor = UIColor.blackColor;
    contentView.userInteractionEnabled = YES;
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self.displayView);
        make.height.ca_offset(44);
    }];
    
    NSArray *imgNames = @[@"Home_1", @"Home_2", @"Home_3", @"Home_4"];
    NSMutableArray *imgViews = [NSMutableArray arrayWithCapacity:imgNames.count];
    for (int i = 0; i < imgNames.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imgNames[i]]];
        [contentView addSubview:imageView];
        [imgViews addObject:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.ca_offset(15);
            if (i == 0) {
                make.left.ca_offset(15);
            } else {
                UIImageView *preImg = imgViews[i - 1];
                make.left.mas_equalTo(preImg.mas_right).mas_offset(5);
            }
        }];
    }
    
    _count = [[kUserModel getPhotoCount] integerValue];
    
    _numLbl = [UILabel new];
    _numLbl.text = [NSString stringWithFormat:@"%ld/10", _count];
    _numLbl.font = CAFontScaleRegular(21);
    _numLbl.textColor = UIColor.flatWhiteColor;
    [contentView addSubview:_numLbl];
    [_numLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-16);
        make.centerY.mas_equalTo(imgViews.firstObject);
    }];
    
    UIButton *switchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [switchBtn setImage:[UIImage imageNamed:@"Home_Switch"] forState:UIControlStateNormal];
    [switchBtn setImage:[UIImage imageNamed:@"Home_Switch"] forState:UIControlStateSelected];
    [switchBtn addTarget:self action:@selector(switchBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:switchBtn];
    [switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.displayView).ca_offset(74.5);
        make.right.mas_equalTo(self.displayView).ca_offset(-15.5);
    }];
    
    
    UIView *bottomView = [UIView new];
    bottomView.backgroundColor = UIColor.blackColor;
    [self addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.displayView);
        make.height.ca_offset(49.5);
    }];
    
    NSArray *btnImgs = @[@"Home_B_1", @"Home_B_2", @"Home_B_3"];
    NSArray *btnTitles = @[@"相册", @"拍照", @"导入"];
    NSMutableArray *btns = [NSMutableArray arrayWithCapacity:btnImgs.count];
    for (int i = 0 ; i < btnImgs.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:btnImgs[i]] forState:UIControlStateNormal];
        button.tag = i;
        [button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [bottomView addSubview:button];
        [btns addObject:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_offset(45);
            make.bottom.mas_offset(-9);
        }];
        UILabel *label = [UILabel new];
        label.text = btnTitles[i];
        label.font = CAFontScaleRegular(11);
        label.textColor = UIColor.flatWhiteColor;
        label.textAlignment = NSTextAlignmentCenter;
        [bottomView insertSubview:label belowSubview:button];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(-9);
            make.centerX.mas_equalTo(button);
        }];
    }
    [btns mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:45 leadSpacing:41.5 tailSpacing:41.5];
}

- (void)buttonEvent:(UIButton *)button {
    NSUInteger tag = button.tag;
    CABlock(self.btnBlock, tag);
}

- (void)switchBtnEvent:(UIButton *)button {
    button.selected = !button.isSelected;
    CABlock(self.switchBlock, button.isSelected);
}

#pragma mark -- STDSRenderViewDelegate
- (void)onReadyRenderView:(STDSRenderView *)view {
    if (view == _displayView) {
        _compose = _displayView.renderer.addCompose;
        [view.renderer addSolidLayerWithColorRed:0.0 green:0.0 blue:0.0];
    }
}

- (void)setCount:(NSInteger)count {
    _count = count;
    _numLbl.text = [NSString stringWithFormat:@"%ld/10", _count];

}
@end
